﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Staticage.Util {
    /// <summary>
    /// 文件帮助类
    /// </summary>
    public static class FileHelper {

        /// <summary>
        /// 创建一个新文件，在其中写入指定的字节数组，然后关闭该文件。如果目标文件已存在，则覆盖该文件。
        /// </summary>
        /// <param name="filePath">要写入的文件路径。</param>
        /// <param name="bytes"> 要写入文件的字节。</param>
        public static void SaveFile(string filePath, byte[] bytes) {
            CreateDirectory(Path.GetDirectoryName(filePath));
            File.WriteAllBytes(filePath, bytes);
        }

        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="directory">目录路径</param>
        public static void CreateDirectory(string directory) {
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);
        }


        /// <summary>
        /// 拷贝文件夹
        /// </summary>
        /// <param name="source">源文件夹</param>
        /// <param name="destination">目标文件夹</param>
        public static void CopyDirectory(string source, string destination) {
            string copyedDirectory = Path.Combine(destination, Path.GetFileName(source));
            CreateDirectory(copyedDirectory);

            foreach (string file in Directory.GetFiles(source)) {
                File.Copy(file, Path.Combine(copyedDirectory, Path.GetFileName(file)), true);
            }

            foreach (string each in Directory.GetDirectories(source)) {
                CopyDirectory(each, copyedDirectory);
            }
        }
    }
}
